#pragma once
#include "afn0dbase.h"

#pragma pack(1)
class ActiveAfn0df3 : public Afn0dfbase
{
public:
	CommonA20 _time;
public:
	inline ActiveAfn0df3(uint16_t pn=0)
	:Afn0dfbase(3, pn)
	{
	}
	inline uint16_t length(void)const
	{
		return sizeof(*this);
	}
	inline Mapss& get(Mapmss& mss)const
	{
		Mapss& m = mss[(int)pnfn];
		m["Fn"]   = Fn();
		m["Pn"]   = Pn();
		m["_time"] = _time.toStringU();
		return m;
	}
	inline bool set(const Mapss& m)
	{
		if(!m.have("Pn")
		|| !m.have("_time")
		|| !m.equal("Fn", Fn()))
		{
			return false;
		}
		int pn = 0;
		Mapss M = m;
		Pn(M["Pn"].toint());
		M["_time"] >> _time;
		return true;
	}};

class PasiveAfn0df3 : public Afn0dfbase
{
public:
	enum{RATE_MIN = 1, RATE_MAX = 12};
public:
	CommonA20 _froztime;
	CommonA15 _copytime;
	uint8_t   _rate;
	CommonA23 _fwttamd;			//forward total active max demand;
	CommonA23 _fwamd[RATE_MAX];	//forward active max demand[rate_max];
	CommonA17 _fwttamdt;		//forward total active max demandtime;
	CommonA17 _fwamdt[RATE_MAX];//forward active max demand time[rate_max];
	CommonA23 _fwttrmd;			//forward total reactive max demand;
	CommonA23 _fwrmd[RATE_MAX];	//forward reactive max demand[rate_max];
	CommonA17 _fwttrmdt;		//forward total reactive max demand time;
	CommonA17 _fwrmdt[RATE_MAX];//forward reactive max demand time[rate_max];
public:
	PasiveAfn0df3(uint16_t pn=0)
	:Afn0dfbase(3, pn),_rate(RATE_MAX)
	{
	}
	inline Mapss& get(Mapmss& mss)const
	{
		Mapss& m = mss[(int)pnfn];
		return m;
	}
	inline uint16_t length(void)const
	{
		if(_rate >= RATE_MIN && _rate <= RATE_MAX)
		{
			return sizeof(PasiveAfn0df3)
				 - (RATE_MAX - _rate) * sizeof(CommonA23) 
				 - (RATE_MAX - _rate) * sizeof(CommonA17)
				 - (RATE_MAX - _rate) * sizeof(CommonA23) 
				 - (RATE_MAX - _rate) * sizeof(CommonA17)
				 ;
		}
	}
	inline void rate(uint8_t r)
	{
		if(r >= RATE_MIN && r <= RATE_MAX)
		{
			_rate = r;
		}
	}
	inline bool set(const Mapss& m)
	{
	////_fwamd_
	////_fwamdt_
	////_fwrmd_
	////_fwrmdt_
		if(!m.have("Pn")
		|| !m.equal("Fn", 3)
		|| !m.have("_froztime")
		|| !m.have("_copytime")
		|| !m.have("_rate")	  
		|| !m.have("_fwttamd")  
		|| !m.have("_fwttamdt")  
		|| !m.have("_fwttrmd") 
		|| !m.have("_fwttrmdt") )
		{
			cout << __func__ << "," << __LINE__ << endl;
			return false;
		}
		Mapss M = m;

		Pn(M["Pn"].toint());
		M["_froztime"] >> _froztime;
		M["_copytime"] >> _copytime;
		M["_rate"	 ] >> _rate;
		M["_fwttamd" ] >> _fwttamd;
		M["_fwttamdt"] >> _fwttamdt;	
		M["_fwttrmd" ] >> _fwttrmd;
		M["_fwttrmdt"] >> _fwttrmdt;
		for(int i = 0; i < _rate; i++)
		{
			sstream fwamd ;
			sstream fwamdt;
			sstream fwrmd ;
			sstream fwrmdt;

			cout <<  i << endl;
			fwamd  << "_fwamd_"  << i;
			fwamdt << "_fwamdt_" << i;
			fwrmd  << "_fwrmd_"  << i;
			fwrmdt << "_fwrmdt_" << i;

			M[fwamd .str()] >> _fwamd [i];	
			M[fwamdt.str()] >> _fwamdt[i];
			M[fwrmd .str()] >> _fwrmd [i];
			M[fwrmdt.str()] >> _fwrmdt[i];
		}
		cout << hexBySize(_froztime) << endl;
		cout << hexBySize(_copytime) << endl;
		return true;
	}
};
#pragma pack(0)
